<?php
/**
 * Created by PhpStorm.
 * User: JYuans
 * Date: 2019/11/27
 * Time: 16:10
 */
namespace Bstage\Controller;

class DepartmentController extends BstageController {
    /**
     * 查询部门列表方法
     */
    public function departmentQuery(){
        //分页
        $now_page = $_REQUEST['now_page']?$_REQUEST['now_page']:1;//默认第一页
        $page_size = $_REQUEST['page_size']?$_REQUEST['page_size']:50;//默认每页50条
        //是否获取全部
        $is_all = $_REQUEST["is_all"]?$_REQUEST["is_all"]:0;
        //查询条件
        $where = array();
        //筛选姓名
        if($_REQUEST["search_name"]){
            $where['name'] = ['like','%'.$_REQUEST['search_name'].'%'];
        }

        $field = "depart_id,name,add_time";
        if($is_all){
            $list_data = M('Department')
                ->where($where)
                ->field($field)
                ->order('add_time DESC')
                ->select();
        }else{
            $list_data = M('Department')
                ->where($where)
                ->field($field)
                ->order('add_time DESC')
                ->page($now_page,$page_size)
                ->select();
        }

        if(!$list_data) {
            $this->returnJson(503,'没有更多数据');
        }

        $current_time = time();
        foreach($list_data as $key=>$value){
            $list_data[$key]['temporary_account_count'] = M("User")
                ->where(['depart_id'=>$value['depart_id'],'user_type'=>0,'use_start_time'=>['lt',$current_time],'use_end_time'=>['gt',$current_time]])
                ->count();

            $list_data[$key]['add_time'] = changeDateTime($value['add_time']);
        }

        $count = M('Department')
            ->where($where)
            ->count();

        $return_data = array();
        $return_data['list_data'] = $list_data;
        $return_data['count'] = $count;
        $return_data['now_page'] = $now_page;
        $return_data['page_size'] = $page_size;
        $return_data['max_page'] = ceil($count / $page_size);

        $this->successReturn("获取成功",$return_data);
    }

    /**
     * 添加部门数据方法
     */
    public function addDepartmentAction(){
        $new_data = array();
        $new_data['name'] = $_REQUEST["name"]?$_REQUEST["name"]:$this->errorReturn("请输入部门名称");

        $new_data['add_time'] = time();

        $add_result = M("Department")
            ->add($new_data);
        if(!$add_result){
            $this->errorReturn("添加失败，请重新尝试");
        }

        $this->successReturn("添加成功");
    }

    /**
     * 修改部门数据方法
     */
    public function editDepartmentAction(){
        $depart_id = $_REQUEST["depart_id"]?$_REQUEST["depart_id"]:$this->errorReturn("缺少部门ID");

        $new_data = array();
        $new_data['name'] = $_REQUEST["name"]?$_REQUEST["name"]:$this->errorReturn("请输入部门名称");
        $new_data['update_time'] = time();

        $update_result = M("Department")
            ->where(['depart_id'=>$depart_id])
            ->save($new_data);
        if($update_result){
            $this->successReturn("修改成功");
        }else{
            $this->errorReturn("修改失败，请重新尝试");
        }
    }

    /**
     * 删除部门数据方法
     */
    public function deleteDepartmentAction(){
        $id_str = $_REQUEST["id_str"]?$_REQUEST["id_str"]:$this->errorReturn("请选择需要删除的部门");

        $delete_result = M("Department")
            ->where(['depart_id'=>['in',$id_str]])
            ->delete();
        if($delete_result){
            $this->successReturn("删除成功");
        }else{
            $this->errorReturn("删除失败，请重新尝试");
        }
    }

    /**
     * 获取部门数据方法
     */
    public function getDepartmentDataAction(){
        $depart_id = $_REQUEST["depart_id"]?$_REQUEST["depart_id"]:$this->errorReturn("缺少部门ID");

        $data = M("Department")
            ->where(['depart_id'=>$depart_id])
            ->find();
        if($data){
            $this->successReturn("获取成功",$data);
        }else{
            $this->errorReturn("获取数据失败，请重新尝试");
        }
    }
}